package GUITOOl;

import GUI.ChatFrame;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import static java.awt.Image.SCALE_DEFAULT;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

/**
 * The type My image lable.
 *
 * @author dzr
 * @version 1.0
 */
public class MyImageLabel extends JLabel implements MouseListener {
    private Window parent;
    private boolean isJFrame;
    private String path;
    private ImageIcon img;
    private int width = 100;
    private int height = 100;

    /**
     * Instantiates a new My image lable.
     *
     * @param path   the path
     * @param parent the parent
     */
    public MyImageLabel(String path, JFrame parent) {
        this.path = path;
        this.img = new ImageIcon(path);
        this.parent = parent;
        this.isJFrame = true;
        img = new ImageIcon(img.getImage().getScaledInstance(width, height, SCALE_DEFAULT));
        this.setIcon(img);
        this.addMouseListener(this);
    }

    /**
     * Instantiates a new My image lable.
     *
     * @param path   the path
     * @param parent the parent
     */
    public MyImageLabel(String path, JDialog parent) {
        this.path = path;
        this.img = new ImageIcon(path);
        this.parent = parent;
        img = new ImageIcon(img.getImage().getScaledInstance(width, height, SCALE_DEFAULT));
        this.setIcon(img);
        this.addMouseListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2){
            MyImageDialog dialog;
            if (isJFrame){
                dialog = new MyImageDialog((ChatFrame) parent);
            }else{
                dialog = new MyImageDialog((JDialog) parent);
            }
            dialog.setTitle(new File(path).getName());
            dialog.setImagePath(path);
            dialog.setBounds(0, 0, dialog.getImgWidth(), dialog.getImgHeight());
            dialog.setVisible(true);
            dialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}

/**
 * The type My image dialog.
 *
 * @author dzr
 * @version 1.0
 */
class MyImageDialog extends JDialog{
    private Image image;
    private int imgWidth;
    private int imgHeight;

    /**
     * Gets img width.
     *
     * @return the img width
     */
    public int getImgWidth() {
        return imgWidth;
    }

    /**
     * Sets img width.
     *
     * @param imgWidth the img width
     */
    public void setImgWidth(int imgWidth) {
        this.imgWidth = imgWidth;
    }

    /**
     * Gets img height.
     *
     * @return the img height
     */
    public int getImgHeight() {
        return imgHeight;
    }

    /**
     * Sets img height.
     *
     * @param imgHeight the img height
     */
    public void setImgHeight(int imgHeight) {
        this.imgHeight = imgHeight;
    }

    /**
     * Instantiates a new My image dialog.
     *
     * @param owner the owner
     */
    public MyImageDialog(Frame owner) {
        super(owner);
    }

    /**
     * Instantiates a new My image dialog.
     *
     * @param owner the owner
     */
    public MyImageDialog(JDialog owner) {
        super(owner);
    }

    /**
     * Sets image path.
     *
     * @param imgPath the img path
     */
    public void setImagePath(String imgPath) {
        try {
            image = ImageIO.read(new FileInputStream(imgPath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        setImgWidth(image.getWidth(this));
        setImgHeight(image.getHeight(this));
    }

    /**
     * To paint image
     * @param g image
     */
    @Override
    public void paint(Graphics g) {
        int x = 0;
        int y = 0;
        if (null == image) {
            return;
        }

        g.drawImage(image, x, y, image.getWidth(this), image.getHeight(this),
                this);
    }
}